#include <memory.h>
#include <stdio.h>
#include <stdlib.h>

#define QUO 0
#define RES 1

#define DIF 0
#define BOR 1

unsigned int N, N4;
unsigned char a[10240], b[10240], c[10240];

unsigned char div5[50][2];

unsigned char div25[250][2];

unsigned short div239[2390][2];

unsigned char difference[11][11][2];

void PRECALC_DIVIDE5() {
     unsigned char i;
     for( i = 0; i < 50; i++) {
         div5[i][QUO] = i / 5;
         div5[i][RES] = (i - div5[i][QUO] * 5) * 10;
     }
}

void PRECALC_DIVIDE25() {
     unsigned char i;
     for( i = 0; i < 250; i++) {
         div25[i][QUO] = i / 25;
         div25[i][RES] = (i - div25[i][QUO] * 25) * 10;
     }
}

void PRECALC_DIVIDE239() {
     unsigned short i;
     for( i = 0; i < 2390; i++) {
         div239[i][QUO] = i / 239;
         div239[i][RES] = (i - div239[i][QUO] * 239) * 10;
     }
}

void PRECALC_SUBTRACT()
{
    unsigned i, j;
    for (i = 0; i <= 10; i++)
    {
        for (j = 0; j <= 10; j++)
        {
            if (i < j)
            {
                difference[i][j][DIF] = i + 10 - j;
                difference[i][j][BOR] = 1;
            }
            else {
                difference[i][j][DIF] = i - j;
                difference[i][j][BOR] = 0;
            }
        }
    }
}

void DIVIDE5( char *x )
{
    unsigned k;
    unsigned char r, u;

    r = 0;
    for( k = 0; k <= N4; k++ )
    {
        u = r + x[k];
        x[k] = div5[u][QUO];
        r = div5[u][RES];
    }
}

void DIVIDE25( char *x )
{
    unsigned k;
    unsigned char r, u;

    r = 0;
    for( k = 0; k <= N4; k++ )
    {
        u = r + x[k];
        x[k] = div25[u][QUO];
        r = div25[u][RES];
    }
}

void DIVIDE239( char *x )
{
    unsigned k;
    unsigned short r, u;

    r = 0;
    for( k = 0; k <= N4; k++ )
    {
        u = r + x[k];
        x[k] = div239[u][QUO];
        r = div239[u][RES];
    }
}

void LONGDIV( char *x, int n )
{
    int j, k;
    unsigned q, r, u;
    long v;

    if( n < 6553 )
    {
        r = 0;
        for( k = 0; k <= N4; k++ )
        {
            u = r * 10 + x[k];
            q = u / n;
            r = u - q * n;
            x[k] = q;
        }
    }
    else
    {
        r = 0;
        for( k = 0; k <= N4; k++ )
        {
            if( r < 6553 )
            {
                u = r * 10 + x[k];
                q = u / n;
                r = u - q * n;
            }
            else
            {
                v = (long) r * 10 + x[k];
                q = v / n;
                r = v - q * n;
            }
            x[k] = q;
        }
    }
}

void MULTIPLY( char *x, int n )
{
    int j, k;
    unsigned q, r, u;
    long v;
    r = 0;
    for( k = N4; k >= 0; k-- )
    {
        q = n * x[k] + r;
        r = q / 10;
        x[k] = q - r * 10;
    }
}

void SET( char *x, int n )
{
    memset( x, 0, N4 + 1 );
    x[0] = n;
}

void SUBTRACT( char *x, char *y, char *z )
{
    int k;
    unsigned char i, j;
    for( k = N4; k >= 0; k-- )
    {
        i = y[k];
        j = z[k];
        x[k] = difference[i][j][QUO];
        z[k-1] += difference[i][j][RES];
    }
}


void calculate( void );
void progress( void );
void epilog( void );


int main( int argc, char *argv[] )
{
    int i;
    N = 10000;

    PRECALC_DIVIDE5();
    PRECALC_DIVIDE25();
    PRECALC_DIVIDE239();
    PRECALC_SUBTRACT();

    setbuf(stdout, NULL);

    calculate();

    epilog();

    return 0;
}

void calculate( void )
{
    int j;

    N4 = N + 4;

    SET( a, 0 );
    SET( b, 0 );

    for( j = 2 * N4 + 1; j >= 3; j -= 2 )
    {
        SET( c, 1 );
        LONGDIV( c, j );

        SUBTRACT( a, c, a );
        DIVIDE25( a );

        SUBTRACT( b, c, b );
        DIVIDE239( b );
        DIVIDE239( b );

        progress();
    }

    SET( c, 1 );

    SUBTRACT( a, c, a );
    DIVIDE5( a );

    SUBTRACT( b, c, b );
    DIVIDE239( b );

    MULTIPLY( a, 4 );
    SUBTRACT( a, a, b );
    MULTIPLY( a, 4 );

    progress();
}

/*

 N = 10000
 A = 0
 B = 0
 J = 2 * (N + 4) + 1
 FOR J = J TO 3 STEP -2
     A = (1 / J - A) / 5 ^ 2
     B = (1 / J - B) / 239 ^ 2
 NEXT J
 A = (1 - A) / 5
 B = (1 - B) / 239
 PI = (A * 4 - B) * 4

*/

void progress( void )
{
    printf(".");
}

void epilog( void )
{
    int j;

    {
        fprintf( stdout, " \n3.");
        for( j = 1; j <= N; j++ )
        {
            fprintf( stdout, "%d", a[j]);
            if( j % 5  == 0 )
                if( j % 50 == 0 )
                    if( j % 250  == 0 )
                        fprintf( stdout, "    <%d>\n\n   ", j );
                    else
                        fprintf( stdout, "\n   " );
                else
                    fprintf( stdout, " " );
        }
    }
}

